package com.yuexun.dhdoctor.module.commun.adapter;

import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.AbsListView;
import android.widget.CheckBox;


import com.yuexun.dhdoctor.R;
import com.yuexun.dhdoctor.config.AppConfig;
import com.yuexun.dhdoctor.module.Contract.bean.ChildDepartmentsBean;
import com.yuexun.dhdoctor.module.Contract.bean.GroupDataBean;

import org.kymjs.kjframe.utils.PreferenceHelper;
import org.kymjs.kjframe.widget.AdapterHolder;
import org.kymjs.kjframe.widget.KJAdapter;

import java.util.ArrayList;
import java.util.Collection;

/**
 * Created by work-jx on 2016/7/19.
 */
public class DepartAndGroup_Depart_Adapter extends KJAdapter<GroupDataBean> {

    private CheckBox checkBox;
    private Handler handler;
    private ArrayList selectValues;

    //0：不能选择自己  1：能选择自己
    private int isSelectMe;

    public DepartAndGroup_Depart_Adapter(AbsListView view, Collection<GroupDataBean> mDatas, int itemLayoutId,
                                         ArrayList selectValues, Handler handler, int isSelectMe) {
        super(view, mDatas, itemLayoutId);
        this.handler = handler;
        this.selectValues = selectValues;
        this.isSelectMe = isSelectMe;
    }

    @Override
    public void convert(AdapterHolder adapterHolder, final GroupDataBean childDepartmentsBean, boolean b, final int position) {
        adapterHolder.setText(R.id.item_department_name, childDepartmentsBean.getDepartment_name());
        adapterHolder.setText(R.id.item_department_count, childDepartmentsBean.getBmrs() + "人");
        checkBox = adapterHolder.getView(R.id.add_checkbox);
        checkBox.setVisibility(View.VISIBLE);

//        if (childDepartmentsBean.getUsers() == null || childDepartmentsBean.getUsers().equals("[]")) {
//            checkBox.setButtonDrawable(R.drawable.add_member_unselect);
//            checkBox.setEnabled(false);
//        } else {
            if (isSelectMe == 0) {
                if (PreferenceHelper.readString(mCxt, AppConfig.USER_INFO, AppConfig.DEPARTMENTID).equals(childDepartmentsBean.getDepartment_id() + "")) {
                    checkBox.setButtonDrawable(R.drawable.add_member_unselect);
                    checkBox.setEnabled(false);
                } else {
                    checkBox.setButtonDrawable(R.drawable.addmember_check);
                    checkBox.setEnabled(true);
                }
            } else {
                checkBox.setButtonDrawable(R.drawable.addmember_check);
                checkBox.setEnabled(true);
            }
//        }

        checkBox.setChecked(false);

        for (int i = 0; i < selectValues.size(); i++) {
            if (selectValues.get(i) instanceof ChildDepartmentsBean) {
                if (((ChildDepartmentsBean) selectValues.get(i)).getDepartment_id() == childDepartmentsBean.getDepartment_id()) {
                    checkBox.setChecked(true);
                }
            }
        }

        checkBox.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                if (!((CheckBox) view).isChecked()) {
                    selectValues.remove(childDepartmentsBean);
                    checkBox.setChecked(false);
//                    notifyDataSetChanged();
                    Message msg = new Message();
                    msg.what = 10003;
                    handler.sendMessage(msg);
                } else {
                    selectValues.clear();
                    selectValues.add(childDepartmentsBean);
                    checkBox.setChecked(true);
                    Message msg = new Message();
                    msg.what = 10003;
                    handler.sendMessage(msg);
                    notifyDataSetChanged();
                }
            }
        });
    }
}
